home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / msm-1 / common.sit / filepart.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  5.6 KB  |  304 lines  |  [TEXT/MPS ]

  1. /*
  2.  * This file contains fparse(), makename(), and smatch().
  3.  */
  4. #include <ctype.h>
  5. #include "::h:gsupport.h"
  6.  
  7. /*
  8.  * The following code is operating-system dependent [@filepart.01].  Define the
  9.  *  characters that terminate a file name prefix.
  10.  */
  11.  
  12. #if PORT
  13. #define Prefix "/"
  14. Deliberate Syntax Error
  15. #endif                    /* PORT */
  16.  
  17. #if AMIGA
  18. #define Prefix "/:"
  19. #endif                    /* AMIGA */
  20.  
  21. #if ARM
  22. #define Prefix ".:"
  23. #endif                    /* ARM */
  24.  
  25. #if ATARI_ST
  26. #define Prefix "/:\\"
  27. #endif                    /* ATARI_ST */
  28.  
  29. #if MSDOS || OS2
  30. #define Prefix "/:\\"
  31. #endif                    /* MSDOS || OS2 */
  32.  
  33. #if MACINTOSH
  34. #define Prefix ":"
  35. #endif                    /* MACINTOSH */
  36.  
  37. #if MVS || VM
  38. #define Prefix ""
  39. #endif                    /* MVS || VM */
  40.  
  41. #if UNIX
  42. #define Prefix "/"
  43. #endif                    /* UNIX */
  44.  
  45. #if VMS
  46. #define Prefix "]:"
  47. #endif                    /* VMS */
  48.  
  49. /*
  50.  * End of operating-system specific code.
  51.  */
  52.  
  53. /*
  54.  * fparse - break a file name down into component parts.
  55.  *  Result is a pointer to a struct of static pointers good until the next call.
  56.  */
  57. struct fileparts *fparse(s)
  58. char *s;
  59.    {
  60.    static char buf[MaxFileName+2];
  61.    static struct fileparts fp;
  62.    int n;
  63.    char *p, *q;
  64.  
  65. #if ARM
  66.    static char buf[MaxFileName+2];
  67.    static struct fileparts fp;
  68.    char *p;
  69.    char *ext = 0;
  70.    char *extend = 0;
  71.    char *dirend = 0;
  72.    char *s1;
  73.    char *bp = buf;
  74.  
  75.    /* First, skip any filing system prefix */
  76.    s1 = index(s,':');
  77.    if (s1 == NULL)
  78.       s1 = s;
  79.    else
  80.       ++s1;
  81.  
  82.    /* Now, scan backwards through the filename, looking for dots.
  83.     * Record the positions of the final two, for later use.
  84.     */
  85.    p = s1 + strlen(s1);
  86.    fp.name = 0;
  87.    
  88.    while (--p > s1)
  89.    {
  90.          if (*p != '.')
  91.             continue;
  92.  
  93.       if (fp.name == NULL)
  94.       {
  95.          fp.name = p + 1;
  96.          extend = p;
  97.       }
  98.       else
  99.       {
  100.          ext = p + 1;
  101.          dirend = p;
  102.          break;
  103.       }
  104.    }
  105.  
  106.    /* This is the simple case. The filename is a simple name, with no
  107.     * directory part. The extension is therefore null, and the directory
  108.     * is just the filing system prefix, if any.
  109.     */
  110.    if (fp.name == NULL)
  111.    {
  112.       fp.name = s1;
  113.  
  114.       if (s1 == s)
  115.       {
  116.          fp.ext = "";
  117.          fp.dir = "";
  118.       }
  119.       else
  120.       {
  121.          fp.ext = "";
  122.          strncpy(buf, s, s1 - s);
  123.          buf[s1-s] = '\0';
  124.          fp.dir = buf;
  125.       }
  126.  
  127.       return &fp;
  128.    }
  129.  
  130.    /* Now worry about the more complicated cases. First, check the
  131.     * supposed extension, to see if it is one of the valid cases,
  132.     * SourceSuffix, U1Suffix, U2Suffix, or USuffix. For this code
  133.     * to work, these four defined values must start with a dot, and
  134.     * be all in lower case.
  135.     */
  136.    *buf = '.';
  137.    bp = buf + 1;
  138.  
  139.    for (p = ext ? ext : s1; p < extend; ++p)
  140.    {
  141.       *bp++ = tolower(*p);
  142.    }
  143.  
  144.    *bp++ = '\0';
  145.  
  146.    if (strcmp(buf,SourceSuffix) == 0 || strcmp(buf,U1Suffix) == 0
  147.     || strcmp(buf,U2Suffix) == 0 || strcmp(buf,USuffix) == 0)
  148.    {
  149.       fp.ext = buf;
  150.    }
  151.    else
  152.    {
  153.       fp.ext = "";
  154.       bp = buf;
  155.       dirend = extend;
  156.    }
  157.  
  158.    /* We now have the name and extension sorted out. So we just need
  159.     * to copy the directory part into buf (at bp), and set fp.dir.
  160.     */
  161.    if (dirend == NULL)
  162.    {
  163.       if (s1 == s)
  164.          fp.dir = "";
  165.       else
  166.       {
  167.          fp.dir = bp;
  168.  
  169.          while (s < s1)
  170.             *bp++ = *s++;
  171.  
  172.          *bp = '\0';
  173.       }
  174.    }
  175.    else
  176.    {
  177.       fp.dir = bp;
  178.  
  179.       while (s <= dirend)
  180.          *bp++ = *s++;
  181.  
  182.       *bp = '\0';
  183.    }
  184.  
  185.    return &fp;
  186.  
  187. #else                    /* ARM */
  188.  
  189. #if MVS
  190.    static char extbuf [MaxFileName+2] ;
  191.  
  192.    p = index(s, '(');
  193.    if (p) {
  194.       fp.member = p+1;
  195.       memcpy(extbuf, s, p-s);
  196.       extbuf [p-s]  = '\0';
  197.       s = extbuf;
  198.    }
  199.    else fp.member = s + strlen(s);
  200. #endif                    /* MVS */
  201.  
  202.    q = s;
  203.    fp.ext = p = s + strlen(s);
  204.    while (--p >= s) {
  205.       if (*p == '.' && *fp.ext == '\0')
  206.          fp.ext = p;
  207.       else if (index(Prefix,*p)) {
  208.          q = p+1;
  209.          break;
  210.          }
  211.       }
  212.  
  213.    fp.dir = buf;
  214.    n = q - s;
  215.    strncpy(fp.dir,s,n);
  216.    fp.dir[n] = '\0';
  217.    fp.name = buf + n + 1;
  218.    n = fp.ext - q;
  219.    strncpy(fp.name,q,n);
  220.    fp.name[n] = '\0';
  221.  
  222. #if VMS
  223.    /* if a version is included, get separate extension and version */
  224.    if (p = index(fp.ext, ';')) {
  225.       fp.version = p;
  226.       p = fp.ext;
  227.       fp.ext = fp.name + n + 1;
  228.       n = fp.version - p;
  229.       strncpy(fp.ext, p, n);
  230.       fp.ext[n] = '\0';
  231.       }
  232.    else
  233.       fp.version = fp.name + n;        /* point version to '\0' */
  234. #endif                                  /* VMS */
  235.  
  236.    return &fp;
  237. #endif                    /* ARM */
  238.    }
  239.  
  240. /*
  241.  * makename - make a file name, optionally substituting a new dir and/or ext
  242.  */
  243. char *makename(dest,d,name,e)
  244. char *dest, *d, *name, *e;
  245.    {
  246.    struct fileparts fp;
  247.    fp = *fparse(name);
  248.    if (d != NULL)
  249.       fp.dir = d;
  250.    if (e != NULL)
  251.       fp.ext = e;
  252.  
  253. #if ARM
  254.    {
  255.       char *p = (*fp.ext ? fp.ext + 1 : "");
  256.       sprintf(dest, "%s%s%s%s", fp.dir, p, (*p ? "." : ""), fp.name);
  257.    }
  258.  
  259. #else                    /* ARM */
  260.  
  261. #if MVS
  262. #if SASC
  263.    {
  264.       char *colons;
  265.       colons = strstr(fp.name, ":::");
  266.       if (colons) {
  267.          memcpy(colons+1, e+1, 2);
  268.          fp.ext = "";
  269.       }
  270.    }
  271. #endif                    /* SASC */
  272.    if (*fp.member)
  273.       sprintf(dest,"%s%s%s(%s", fp.dir, fp.name, fp.ext, fp.member);
  274.    else
  275. #endif                    /* MVS */
  276.  
  277.    sprintf(dest,"%s%s%s",fp.dir,fp.name,fp.ext);
  278. #endif                    /* ARM */
  279.  
  280.    return dest;
  281.    }
  282.  
  283. /*
  284.  * smatch - case-insensitive string match - returns nonzero if they match
  285.  */
  286. int smatch(s,t)
  287. char *s, *t;
  288.    {
  289.    char a, b;
  290.    for (;;) {
  291.       while (*s == *t)
  292.          if (*s++ == '\0')
  293.             return 1;
  294.          else
  295.             t++;
  296.       a = *s++;
  297.       b = *t++;
  298.       if (isupper(a))  a = tolower(a);
  299.       if (isupper(b))  b = tolower(b);
  300.       if (a != b)
  301.          return 0;
  302.       }
  303.    }
  304.